package com.quanshu.exchange.ui.gui.message.chat.expand;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.chat.ChatConstant;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by jye on 2017/12/12.
 */

public class ExpandRecyclerView extends RecyclerView {

    private SingleAdapter<ExpandMenu> adapter;

    private BehaviorSubject<String> click = BehaviorSubject.create();

    public BehaviorSubject<String> getClick() {
        return click;
    }

    public ExpandRecyclerView(Context context) {
        super(context);
    }

    public ExpandRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ExpandRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void initExpandMessage(int chatType) {

        List<ExpandMenu> expandMenus = new ArrayList<>();

        ExpandMenu expandMenu = new ExpandMenu();
        expandMenu.setLayoutID(R.drawable.chat_expand_album);
        expandMenu.setTitle("相册");
        expandMenus.add(expandMenu);

        expandMenu = new ExpandMenu();
        expandMenu.setLayoutID(R.drawable.chat_expand_take_photo);
        expandMenu.setTitle("拍照");
        expandMenus.add(expandMenu);

        expandMenu = new ExpandMenu();
        expandMenu.setLayoutID(R.drawable.icon_chat_video);
        expandMenu.setTitle("小视频");
        expandMenus.add(expandMenu);

        // 单聊
        if (chatType == ChatConstant.CHAT_TYPE_SINGLE) {

            expandMenu = new ExpandMenu();
            expandMenu.setLayoutID(R.drawable.chat_expand_video);
            expandMenu.setTitle("视频聊天");
            expandMenus.add(expandMenu);

            expandMenu = new ExpandMenu();
            expandMenu.setLayoutID(R.drawable.chat_expand_audio);
            expandMenu.setTitle("语音聊天");
            expandMenus.add(expandMenu);
        }
        // 群聊
        else if (chatType == ChatConstant.CHAT_TYPE_GROUP) {

        }
        expandMenu = new ExpandMenu();
        expandMenu.setLayoutID(R.drawable.icon_chat_red_package);
        expandMenu.setTitle("红包");
        expandMenus.add(expandMenu);

        setLayoutManager(new GridLayoutManager(getContext(), 4));
        adapter = new SingleAdapter<>(new DataDisplayer<ExpandMenu>(getContext(), R.layout.item_chat_menu) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<ExpandMenu> wrapper, ExpandMenu item, int position) {
                holder.setImage(R.id.chat_menu_image, item.getLayoutID());
                holder.setText(R.id.chat_menu_text, item.getTitle());
                RxViewHelper.getInstance().clicks(holder.getRootView(), aVoid -> click.onNext(item.getTitle()));
            }
        });
        setAdapter(adapter);
        adapter.setData(expandMenus);
    }

    public int getPageSize() {
        if (adapter != null) {
            int size = adapter.getItemCount();
            return (int) Math.ceil(size / 8.0);
        }
        return 0;
    }

}
